;
;moxun
;2015-7-25
;
SetTitleMatchMode, 2
#NoTrayIcon
#SingleInstance, force
#ErrorStdOut
CoordMode, Mouse, Relative
version = AndroidCapturer1.2

capture:

StringLen, ashLen, Android_SDK_HOME
if ashLen < 1
{
    Msgbox, 16, %version% - 错误, 请先配置Android_SDK_HOME`n%Android_SDK_HOME%
	exitapp
}
adbPath = %Android_SDK_HOME%\platform-tools\adb.exe
IfNotExist, %adbPath%
{
	Msgbox, 16, %version% - 错误, 未找到文件`n%adbPath%
	exitapp
}
devicesCount = 0
serialnoList = 
selectedDevice = 
devicesFileSavePath = %A_Temp%\android_devices.txt
FileDelete, %devicesFileSavePath%
commoander = 
RunWait, cmd /c %adbPath% devices -l | find "model:" > %devicesFileSavePath%, , hide
IfNotExist, %devicesFileSavePath%
{
	Msgbox, 16, %version% - 错误, 查询当前已连接的设备失败
	exitapp
}
Loop, read, %devicesFileSavePath%
{
	StringGetPos, p1, A_LoopReadLine, %A_Space%
	StringGetPos, p2, A_LoopReadLine, model:
	StringGetPos, p3, A_LoopReadLine, device:
	StringMid, v1, A_LoopReadLine, 0, p1
	StringMid, v2, A_LoopReadLine, p2 + 7, p3 - p2 - 7
	serialnoList%A_Index% := v1
	Menu, devicesSelectedMenu, add, %v2%, startCaptureFromMenu
	Menu, devicesSelectedMenu, Icon, %v2%, AndroidCapturer.exe, 0, 0
	++devicesCount
}
if devicesCount = 0
{
	Msgbox, 64, %version% - 提示, 请连接手机到本机并进行USB调试授权
	exitapp
}
else if devicesCount > 1
{
	Menu, devicesSelectedMenu, show
	exitapp
}
else
{
	selectedDevice := serialnoList1
	Goto, startCapture
}
return

exitapp

startCaptureFromMenu:
selectedDevice := serialnoList%A_ThisMenuItemPos%
Goto, startCapture
exitapp

startCapture:
	ToolTip, 正在截图`，请稍等...
	RunWait, %adbPath% -s %selectedDevice% shell /system/bin/screencap -p /sdcard/screenshot_tmp, , hide

	picName = %A_YYYY%_%A_MM%_%A_DD%_%A_Hour%_%A_Min%_%A_Sec%.png
	RunWait, %adbPath% -s %selectedDevice% pull /sdcard/screenshot_tmp %A_ScriptDir%/screenshot/%picName%, , hide
	IfNotExist, %A_ScriptDir%/screenshot/%picName%
	{
		Msgbox, 16, %version% - 错误, 截图失败
		exitapp
	}
	ToolTip, 截图完成
	Run, %adbPath% -s %selectedDevice% shell rm /sdcard/screenshot_tmp, , hide
	Run, %A_ScriptDir%/screenshot/%picName%

exitapp
